package cn.tedu.autoshow.website.controller;

import cn.tedu.autoshow.commons.pojo.vo.PageData;
import cn.tedu.autoshow.commons.security.LoginPrincipal;
import cn.tedu.autoshow.commons.web.JsonResult;
import cn.tedu.autoshow.website.pojo.param.AutoShowAddNewParam;
import cn.tedu.autoshow.website.pojo.param.AutoShowUpdateInfoParam;
import cn.tedu.autoshow.website.pojo.vo.AutoShowListItemVO;
import cn.tedu.autoshow.website.pojo.vo.AutoShowStandardVO;
import cn.tedu.autoshow.website.service.IAutoShowService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;


@Api(tags = "04. 车展管理模块")
@RestController
@Slf4j
@RequestMapping("/auto/shows")
public class AutoShowController {
    @Autowired
    private IAutoShowService autoShowService;

    @PostMapping("/add-new")
    @ApiOperation("新增车展")
    //@PreAuthorize("hasAuthority('/wms/auto/shows/add-new')")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid AutoShowAddNewParam autoShowAddNewParam,
                             @ApiIgnore LoginPrincipal loginPrincipal){
        autoShowService.addNew(autoShowAddNewParam,loginPrincipal);
        return JsonResult.ok();
    }


    // http://localhost:9080/auto/shows/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    //@PreAuthorize("hasAuthority('/wms/auto/shows/delete')")
    @ApiOperation("根据ID删除车展")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "车展ID", required = true, dataType = "long")
    })
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的车展ID值！") Long id) {
        log.debug("开始处理【根据ID删除车展】的请求，参数：{}", id);
        autoShowService.deleteById(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/auto/shows/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    //@PreAuthorize("hasAuthority('/wms/auto/shows/update')")
    @ApiOperation("修改车展详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "车展ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的车展ID值！") Long id,
                                           @Valid AutoShowUpdateInfoParam autoShowUpdateInfoParam) {
        log.debug("开始处理【修改车展详情】的请求，ID：{}，新数据：{}", id, autoShowUpdateInfoParam);
        autoShowService.updateById(id,autoShowUpdateInfoParam);
        return JsonResult.ok();
    }


    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用车展")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "车展ID", required = true, dataType = "long")
    })
    public JsonResult setEnable(@PathVariable @Range(min = 1, message = "请提交有效的车展ID值！") Long id) {
        autoShowService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用车展")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "车展ID", required = true, dataType = "long")
    })
    public JsonResult setDisable(@PathVariable @Range(min = 1, message = "请提交有效的车展ID值！") Long id) {
        autoShowService.setDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/auto/shows/9527
    @GetMapping("/{id:[0-9]+}")
    //@PreAuthorize("hasAuthority('/wms/auto/shows/read')")
    @ApiOperation("根据ID查询车展详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "车展ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的车展ID值！") Long id) {
        log.debug("开始处理【根据ID查询车展详情】的请求，参数：{}", id);
        AutoShowStandardVO queryResult = autoShowService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }


    // http://localhost:9180/auto/shows
    @GetMapping("")
    @ApiOperation("查询车展列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult list(@Range(min = 1, message = "查询车展列表失败，请提供正确的页码值！") Integer page,
                           @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("开始处理【查询车展列表】的请求，页码：{}", page);
        log.debug("当事人：{}", loginPrincipal);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<AutoShowListItemVO> pageData = autoShowService.list(page);
        return JsonResult.ok(pageData);
    }

}
